package mydessertstore.web.servlet;

import mydessertstore.domain.Account;
import mydessertstore.service.LogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ShippingFormServlet extends HttpServlet {

    private static final String SHIPPING_FORM = "/WEB-INF/jsp/order/shippingForm.jsp";
    private static final String SIGN_ON_FORM = "/WEB-INF/jsp/account/signon.jsp";

    private Account account;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset = UTF-8");

        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset = UTF-8");

        HttpSession session = request.getSession();
        account = (Account)session.getAttribute("account");

        if (account == null){
            request.getRequestDispatcher(SIGN_ON_FORM).forward(request, response);
        } else{


            //日志功能
            if(account!=null) {

                LogService logService = new LogService();

                //大家根据自己servlet完成的功能把"查看购物车"这里的字符串改成对应的功能
                String logInfo = logService.logInfo(" ") + "设置配送地址" ;
                logService.insertLogInfo(account.getUsername(), logInfo);
            }
            else{
                LogService logService = new LogService();
                String logInfo = logService.logInfo(" ") + " 设置配送地址" ;
                logService.insertLogInfo("未登录用户", logInfo);
            }
            //日志结束

            request.getRequestDispatcher(SHIPPING_FORM).forward(request, response);
        }
    }
}
